package com.sgb.kjwl.view.ui.activity.self.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.utils.Constants;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivityLogisticsInfoBinding;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.OrderEvent;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LogisticsInfoActivity extends AppActivity {

    private ActivityLogisticsInfoBinding binding;
    private String id;

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.setClass(context, LogisticsInfoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_logistics_info);
        binding.include.topLayout.setBackgroundColor(getResources().getColor(R.color.color_FFFFFF));
        binding.include.titleView.setText("物流信息");
        binding.include.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.include.rightView.setVisibility(View.INVISIBLE);

        binding.tvQuerySendGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkContent();
            }
        });
    }


    /**
     * 保存物流信息
     */
    private void checkContent() {
        String content = binding.etInfo.getText().toString();

        DialogHelper.showProgressDialog(this, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getOrderAPI().addLogisticsInfo(id, content).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    confirmOrder();
                } else {
                    MToast.showToast(LogisticsInfoActivity.this, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(LogisticsInfoActivity.this, t.getMessage());
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 确认订单
     */
    private void confirmOrder() {
        DialogHelper.showProgressDialog(this, "请求中", 0, false);
        APIManager.getInstance().getOrderAPI().confirmDelivery(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(LogisticsInfoActivity.this, response.body() != null ? response.body().getMsg() : "请求失败");
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    EventBus.getDefault().postSticky(OrderEvent.make().setMessageTag(EventConstant.EVENT_ORDER_LIST_SEND));
                    EventBus.getDefault().postSticky(OrderEvent.make().setMessageTag(EventConstant.EVENT_ORDER_LIST_RECEIVE));
                    ARouter.getInstance().build(Constants.ARouter.AR_SELL_ORDER_LIST).navigation();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
    }
}
